package com.huangliusong.aop.demo2;

import org.aopalliance.intercept.MethodInvocation;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class MYCgLibProxy implements MethodInterceptor {
    private ProductDao productDao;
    public MYCgLibProxy(ProductDao productDao){
        this.productDao=productDao;
    }

    public Object createProxy(){
        //创建核心类
        Enhancer e=new Enhancer();
        //设置父类
        e.setSuperclass(productDao.getClass());
        //设置回调
        e.setCallback(this);
        //生成代理
        Object o=e.create();
        return o;
    }

    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        if("save".equals(method.getName())){
            System.out.println("权限认证");
            return methodProxy.invokeSuper(o,objects);
        }
        return methodProxy.invokeSuper(o,objects);
    }
}
